#pragma once
#include <queue>
#include <mutex>
#include <condition_variable>

// 异步日志队列,生产消费者
template<class T>
class LockQueue
{
private:
    // 日志队列
    std::queue<T> m_queue;
    std::mutex m_mutex;
    std::condition_variable m_condvariable;
public:
    void Push(const T& data)
    {
        std::lock_guard<std::mutex> lock(m_mutex);
        m_queue.push(data);
        // 唤醒一个条件变量
        m_condvariable.notify_one();
    }
    T Pop()
    {
        // 绑定条件变量锁采用unique_lock
        std::unique_lock<std::mutex> lock(m_mutex);
        while (m_queue.empty()) {
            // 线程进入wait状态,等待条件变量唤醒
            m_condvariable.wait(lock);
        }

        T data = m_queue.front();
        m_queue.pop();
        return data;
    }
};